/**
 * Title: RegManager.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-2-28 上午9:49:53
 * Version 1.0
 */
package com.bchun.gamepk.web.model.manager;

import com.bchun.gamepk.model.UsersInfo;
import com.bchun.gamepk.service.IUsersInfoService;
import com.bchun.gamepk.web.common.ResultCode;
import com.bchun.gamepk.web.common.ResultMessage;
import com.bchun.gamepk.web.model.request.RegRequest;
import com.bchun.gamepk.web.model.resposne.ServerResult;
import com.bchun.gamepk.web.model.resposne.UserRegResponse;
import com.google.gson.Gson;

/** 
 * ClassName: RegManager
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-2-28 上午9:49:53
 */
public class RegManager {
	public UserRegResponse reg(String requestStr, IUsersInfoService usersInfoService) {
		UserRegResponse response = new UserRegResponse();
		ServerResult serverResult = new ServerResult();
		Gson gson = new Gson();
		RegRequest request = null;
		try {
			request = gson.fromJson(requestStr, RegRequest.class);
		} catch (Exception e) {
			serverResult.setResultCode(ResultCode.fail.getValue());
			serverResult.setResultMessage(ResultMessage.invalidJson.getValue());
			response.setServerResult(serverResult);
			return response;
		}
		
		UsersInfo usersInfo = usersInfoService.getByLoginName(request.getLoginName());
		
		if (usersInfo == null)
		{
			usersInfo = new UsersInfo();
			usersInfo.setLoginName(request.getLoginName());
			usersInfo.setUserName(request.getUserName());
			usersInfo.setPassword(request.getPassword());
			usersInfo.setAddress(request.getRecipientAddress());
			usersInfo.setPhoneNumber(request.getMobileNumber());
			usersInfo.setEmail(request.getEmail());
			Object object = usersInfoService.insert(usersInfo);
			
			if (object != null)
			{
				serverResult.setResultCode(ResultCode.success.getValue());
				serverResult.setResultMessage(ResultMessage.suc.getValue());
				response.setServerResult(serverResult);
				return response;
			}
			else
			{
				serverResult.setResultCode(ResultCode.fail.getValue());
				serverResult.setResultMessage(ResultMessage.databaseError.getValue());
				response.setServerResult(serverResult);
				return response;
			}
		}
		serverResult.setResultCode(ResultCode.fail.getValue());
		serverResult.setResultMessage(ResultMessage.loginNameExist.getValue());
		response.setServerResult(serverResult);
		return response;
	}
}
